/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.DAO;
import TOOL.Ulti;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;

/**
 *
 * @author Endlesslove
 */
public  class PhieuNhapSach implements IDaTa{

    public ResultSet GetInfo(String ID)
    {
        try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  ID) ;//makhachhang
            return DAO.CallProc("LayThongTinPhieuNhapSach", params);   
        } 
        catch (Exception ex) 
        {
            return null;
        }
    }
    @Override
    public ResultSet Search(DTO.Idata Obj) 
    {
        try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  ((DTO.PhieuNhapSach)Obj).getID()) ;//masach
            return DAO.CallProc("LayThongTinPhieuNhapSach", params);
            
        } 
        catch (Exception ex) 
        {
            return null;
        }
        //throw new UnsupportedOperationException();
    }

    @Override
    public boolean Update(DTO.Idata Obj) 
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean Delete(DTO.Idata Obj) 
    {
        List<String> params = new ArrayList<>();
        params.add("char:" + ((DTO.PhieuNhapSach)Obj).getID());
        return DAO.CallProc("XoaPhieuNhapSach", params, true);
    }

    @Override
    public void LoadData(Object table) 
    {
        ResultSet res = DAO.CallProc("LayThongTinToanBoPhieuNhapSach");
        Ulti.FillData((JTable)table, res);
    }

    @Override
    public boolean Insert(DTO.Idata Obj) 
    {
        List<String> params = new ArrayList<>();
        params.add("char:" +  ((DTO.PhieuNhapSach)Obj).getID()) ;//ma 
        params.add("Date:" +  ((DTO.PhieuNhapSach)Obj).NgayNhap);//Ngay nhap 
        return DAO.CallProc("ThemPhieuNhapSach", params, true);
    }

    @Override
    public String GenerateID()
    {
        try 
        {
                String mkh = "PN";
                ResultSet res = DAO.CallProc("LayMaPhieuNhapSachCuoi");
                if (res.next())
                { 
                    try
                    {
                        int index  = Integer.parseInt(((String)res.getObject(1)).split("PN")[1].trim());

                        mkh += (index+1)>=10?(index+1)>=100? (index +1) : "0" + (index +1) : "00" + (index + 1);
                    }
                    catch(Exception ie)
                    {
                        return "PN001";
                    }
                  return mkh;
                }
                return "PN001";
        } 
        catch (SQLException ex) 
        {
            return "PN001";
        }
    }
    
    public boolean DeletePN(String MPN)
    {
        try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  MPN) ;//masach
            return DAO.CallProc("XoaPhieuNhapSach", params, true);
            
        } 
        catch (Exception ex) 
        {
            return false;
        }
    }
   
}
